package cn.janescott.zone.portal.util;

import cn.janescott.zone.data.dto.User;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.lang.reflect.Field;
import java.util.*;

/**
 * Created by Scott on 2018/6/28
 */
public abstract class RedirectUtils {

    /**
     * 使用POST重定向
     *
     * @param request
     * @param response
     * @param params     参数
     * @param pojoParams 对象参数
     * @param uri        路径
     */
    @SuppressWarnings("unchecked")
    public static void sendPost(HttpServletRequest request,
                                HttpServletResponse response,
                                String uri,
                                Map<String, Object> params,
                                Object... pojoParams) {
        if (null == params) {
            params = new HashMap<>();
        }
        try {
            response.setContentType("text/html;charset=utf-8");
            PrintWriter out = response.getWriter();
            out.println("<form name='myForm' method='post' action='actual'>");
            if (!CollectionUtils.isEmpty(params)) {
                for (Map.Entry<String, Object> next : params.entrySet()) {
                    Object value = next.getValue();

                }
            }
            out.println("</form>");
            out.println("<script>");
            out.println("window.document.myForm.submit()");
            out.println("</script>");
        } catch (Exception ex) {

        }
    }

    /**
     * 绑定简单参数
     *
     * @param out    输出流
     * @param target 参数对象
     */
    private void bindPlainParam(PrintWriter out, Object target, String name) {

    }

    /**
     * 绑定对象参数
     *
     * @param out    输出流
     * @param target 参数对象
     * @param prefix 前缀
     */
    private void bindPojoParam(PrintWriter out, Object target, String prefix) {
        if (null == target) {
            return;
        }

//        try {
//
//            if (target instanceof List) {
//                List source = (List) target;
//                if (!CollectionUtils.isEmpty(source)) {
//                    for (int i = 0; i < source.size(); i++) {
//                        bindParam(out, source.get(i), prefix + "[" + i + "]");
//                    }
//                }
//            } else if (value instanceof Map) {
//                Map<String, Object> source = (Map<String, Object>) value;
//                if (!CollectionUtils.isEmpty(source)) {
//                    for (Map.Entry<String, Object> subNext : source.entrySet()) {
//                        out.println("<input type='hidden' name='" + subNext.getKey() + "' value='" + subNext.getValue().toString() + "'>");
//                    }
//                }
//            } else {
//                out.println("<input type='hidden' name='" + prefix + "' value='" + String.valueOf(target) + "'>");
//            }
//
//        }

    }

    public static void main(String[] args) {
        PrintStream out = System.out;
//        User user = new User();
//        user.setId(123456789L);
//        user.setName("scott");
//
//        Object obj = user;
//        Class clazz = User.class;

        List list = new ArrayList();
        list.add(new User());
        list.add("scott");

        Object obj = list;
//        if (clazz.isInstance(obj)) {
        try {
            Field[] fields = obj.getClass().getDeclaredFields();
            Field.setAccessible(fields, true);
            for (Field field : fields) {
                out.println(field.getName());
                out.println(field.get(obj));
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
//        }

    }
}
